﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Threading;
using NHibernate;
using NHibernate.Criterion;
using OpenGado.DATA.Entities;

namespace OpenGado.DATA
{
    /// <summary>
    /// Data Control class that manages the relationship between projects and products which is many to many. 
    /// </summary>
    public class ProductProjectDataControl
    {

        private ISession session;
        private static object lockProducProjectControl = new object();
        private static ProductProjectDataControl productProjectDataControl;

        public ISession Session
        {
         get
         {
                 session = FactoryHandler.CreateFactory().OpenSession();
            
             return session;
         }
        }

        public static ProductProjectDataControl Instance
        {
            get
            {
               lock(lockProducProjectControl)
               {
                   if (productProjectDataControl == null)
                   {
                       productProjectDataControl = new ProductProjectDataControl();

                   }
                   return productProjectDataControl;
               }
            }
        }

        public void AddProductToProject(ProjectHasProduct projectProductLink)
        {


        }

        /// <summary>
        /// Gets Project based on its product id
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public Project GetByProductId(int productId)
       {
           ISession tsession = Session;
            ICriteria criteria = tsession.CreateCriteria("ProjectHasProduct")
                .Add(Restrictions.Eq("Product", productId));

          
            IList<Project> projects = criteria.List<Project>();

            tsession.Close();

            return projects[0];
       }

        /// <summary>
        /// Adds Products and their respective quantity to the project list.
        /// </summary>
        /// <param name="project"></param>
        /// <param name="product"></param>
        /// <param name="productQuantity"></param>
        public void AddProductToProject(Project project, Product product, decimal productQuantity)
        {
            ProjectHasProduct pp = new ProjectHasProduct();

            pp.Product = product;
            pp.Project = project;
            pp.ProductQuantity = productQuantity;

            // cehck if this combo already exists. 

            

            // if everything is in order we save the information 
            ISession tsession = Session;
            ITransaction transaction;
            transaction = tsession.BeginTransaction();
            try
            {
                tsession.SaveOrUpdate(pp);
                transaction.Commit();
              
            }
            catch (Exception)
            {
                
               transaction.Rollback();
            }
            finally
            {
                tsession.Close();
            }
           
           
        }
    }
}
